package com.yhy.common.excel2pdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.yhy.common.excel2pdf.constant.PdfFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
/**
 *  基于poi及itext实际 excel 转pdf
 * @Author yanghuiyuan
 * @Date 2023/5/26 20:30
 */
public class CustomExcelConvertPdf implements IExcelConvertPdf {

    private void addWatermark(PdfWriter pdfWriter, String waterMarkText) {
        if(waterMarkText == null) {
            return;
        }
        try {
            // 加入水印
            PdfContentByte waterMar = pdfWriter.getDirectContentUnder();
            // 开始设置水印
            waterMar.beginText();
            // 设置水印透明度
            PdfGState gs = new PdfGState();
            // 设置填充字体不透明度为0.4f
            gs.setFillOpacity(0.1f);
            // 设置水印字体参数及大小                                  (字体参数，字体编码格式，是否将字体信息嵌入到pdf中（一般不需要嵌入），字体大小)
            waterMar.setFontAndSize(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED), 20);//.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED), 20);
            // 设置透明度
            waterMar.setGState(gs);
            // 设置水印对齐方式 水印内容 X坐标 Y坐标 旋转角度
            waterMar.showTextAligned(Element.ALIGN_RIGHT,waterMarkText, 200, 200, 5);
            // 设置水印颜色
            waterMar.setColorFill(BaseColor.PINK);
            //结束设置
            waterMar.endText();
            waterMar.stroke();

            waterMar = null;
            gs = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 转换excel为pdf
     * @param excelPath
     * @param pdfPath
     */
    public void convertPdf(String excelPath, String pdfPath) {
        Document document = null;
        PdfWriter pdfWriter = null;
        try {
            HSSFWorkbook workBook = new HSSFWorkbook(new FileInputStream(excelPath));
            /*Rectangle pageSize = new Rectangle(842.0F, 595.0F);
            Document document = new Document(pageSize);*/
            document = new Document(PageSize.A4.rotate());
            pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
            CustomConvertPdfUtil.convertPdf(workBook, document);

            this.addWatermark(pdfWriter, "简云ERP");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭文档
            if(document != null)
            document.close();
            if(pdfWriter != null)
            pdfWriter.close();
        }
    }

    public void convertPdf(List<String> excelPaths,  OutputStream outputStream) {
        Document document = null;
        PdfWriter pdfWriter = null;
        try {
            /*Rectangle pageSize = new Rectangle(842.0F, 595.0F);
            Document document = new Document(pageSize);*/
            document = new Document(PageSize.A4.rotate());
            pdfWriter = PdfWriter.getInstance(document, outputStream);
            for (String excelPath : excelPaths) {
                HSSFWorkbook workBook = new HSSFWorkbook(new FileInputStream(excelPath));
                CustomConvertPdfUtil.convertPdf(workBook, document);
            }
            this.addWatermark(pdfWriter, "简云ERP");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭文档
            if(document != null)
                document.close();
            if(pdfWriter != null)
                pdfWriter.close();
        }
    }


    public void convertPdf(String excelPath, OutputStream outputStream) {
        Document document = null;
        PdfWriter pdfWriter = null;
        try {
            /*Rectangle pageSize = new Rectangle(842.0F, 595.0F);
            Document document = new Document(pageSize);*/

            HSSFWorkbook workBook = new HSSFWorkbook(new FileInputStream(excelPath));
            /*Rectangle pageSize = new Rectangle(842.0F, 595.0F);
            Document document = new Document(pageSize);*/
            document = new Document(PageSize.A4.rotate());
            pdfWriter = PdfWriter.getInstance(document, outputStream);
            CustomConvertPdfUtil.convertPdf(workBook, document);
            document.close();

            this.addWatermark(pdfWriter, "简云ERP");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭文档
            if(document != null)
                document.close();
            if(pdfWriter != null)
                pdfWriter.close();
        }
    }

    public void convertPdf(InputStream inputStream, OutputStream outputStream) {
        Document document = null;
        PdfWriter pdfWriter = null;
        try {
            HSSFWorkbook workBook = new HSSFWorkbook(inputStream);
            /*Rectangle pageSize = new Rectangle(842.0F, 595.0F);
            Document document = new Document(pageSize);*/
            document = new Document(PageSize.A4.rotate());
            pdfWriter = PdfWriter.getInstance(document, outputStream);
            CustomConvertPdfUtil.convertPdf(workBook, document);
            this.addWatermark(pdfWriter, "简云ERP");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭文档
            if(document != null)
                document.close();
            if(pdfWriter != null)
                pdfWriter.close();
        }
    }

    public void convertPdf(HSSFWorkbook workBook, OutputStream outputStream) {
        Document document = null;
        PdfWriter pdfWriter = null;
        try {
            /*Rectangle pageSize = new Rectangle(842.0F, 595.0F);
            Document document = new Document(pageSize);*/
            document = new Document(PageSize.A4.rotate());
            pdfWriter = PdfWriter.getInstance(document, outputStream);
            CustomConvertPdfUtil.convertPdf(workBook, document);
            this.addWatermark(pdfWriter, "简云ERP");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭文档
            if(document != null)
                document.close();
            if(pdfWriter != null)
                pdfWriter.close();
        }
    }


}
